package com.xiaoyi.ui.fragment.base;

import android.content.Context;
import android.support.v4.app.Fragment;

import me.yokeyword.fragmentation.SupportFragment;

import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.ui.fragment.pic.PicFragment;
import com.xiaoyi.ui.view.lodingdialog.LoadingDialog;
import com.xiaoyi.ui.view.lodingdialog.LoadingDialog2;

/**
 * 懒加载
 */
public abstract class BaseMainFragment extends SupportFragment {
    protected OnBackToFirstListener _mBackToFirstListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnBackToFirstListener) {
            _mBackToFirstListener = (OnBackToFirstListener) context;
        } else {
            throw new RuntimeException( context.toString()   + " must implement OnBackToFirstListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        _mBackToFirstListener = null;
    }

    /**
     * 处理回退事件
     */
    @Override
    public boolean onBackPressedSupport() {
        if (getChildFragmentManager().getBackStackEntryCount() > 1) {
            popChild();
        } else {

            _mBackToFirstListener.onBackToFirstFragment(this);

        }
        return true;
    }

    public interface OnBackToFirstListener {
        void onBackToFirstFragment(Fragment fragment);
    }


    private  LoadingDialog loadingDialog;
    public void showLoadDialog(Context context  ) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(context);
        }
        loadingDialog.show();
        loadingDialog.setMsg("正在获取");
    }

    public void hideLoadDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    private LoadingDialog2 loadingDialog2;
    public void showLoadDialog2(Context context  ) {
        if (loadingDialog2 == null) {
            loadingDialog2 = new LoadingDialog2(context);
        }
        loadingDialog2.show();
        loadingDialog2.setMsg("加载中");
    }

    public void hideLoadDialog2() {
        if (loadingDialog2 != null) {
            loadingDialog2.dismiss();
        }
    }
}
